package com.zsj.mzitu.task;

import com.zsj.mzitu.pojo.Item;
import com.zsj.mzitu.service.ItemService;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class ItemTask {
    @Autowired
    private ItemService itemService;

    @Scheduled(fixedDelay = 100 * 1000)
public void itemTask() throws Exception{
    for(int j =1;j<=259;j++) {
        String url1 = "https://www.mzitu.com/page/" + j + "/";
        Document document = Jsoup.connect(url1).get();
        Elements elements = document.select(".postlist ul li span a");
        String dizhi = "";
        String mingzi = "";
        String url = "";
        for (Element element : elements) {
            //每一个a标签就对应一张图,然后拿到里面的href属性的值
            dizhi = element.attr("href");
            mingzi = element.text();
            System.out.println(dizhi);
            try {
                String[] split = mingzi.split("\"");
                mingzi=split[0]+split[1]+split[2];
                System.out.println("有\"："+mingzi);
            }catch (Exception e) {
                System.out.println("没有\"："+mingzi);
            }
            Item item= new Item();
            item.setTitle(mingzi);
            List<Item> list = this.itemService.findAll(item);
            if(list.size()>0) {
                //如果商品存在，就进行下一个循环，该商品不保存，因为已存在
                System.out.println("这条数据已经存在了...");
                continue;
            }

            item.setCreated(new Date());
            this.itemService.save(item);
            System.out.println(mingzi);

            //1. 获取网络资源地址
            url = dizhi;//修改此地址就可以获取另外的图集了
            Document document1 = Jsoup.connect(url).get();
            //找到表示总共多少页的 <a> 标签
            Elements elements1 = document1.select(".pagenavi a");
            Integer span = Integer.valueOf(elements1.eq(elements1.size() - 2).select("span").text());
            for (Integer i = 1; i <= span; i++) {
                Thread.sleep(1000);
                String url02 = url + "/" + i;//具体每张图对应的页面
                Document document02 = Jsoup.connect(url02).get();
                //找到这个图片列表对应的位置
                String src = "";
                Elements elements02 = document02.select(".main-image p a img");
                for (Element element02 : elements02) {
                    //每一个a标签就对应一张图,然后拿到里面的href属性的值
                    src = element02.attr("src");
                    System.out.println(src);
                }
                //使用这个地址下载图片
                //1.使用Java代码模拟出一个客户端
                CloseableHttpClient httpClient = HttpClients.createDefault();

                //2.创建一个get请求
                HttpGet httpGet = new HttpGet(src);
                //添加头部信息模拟浏览器访问
                httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
                httpGet.setHeader("Accept-Encoding", "gzip, deflate, sdch, br");
                httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
                httpGet.setHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
                httpGet.setHeader("Referer", "https://www.mzitu.com/");//告诉服务器women从哪里来的
                //3.使用客户端执行请求,获取响应
                CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
                //4.获取响应体
                HttpEntity entity = httpResponse.getEntity();
                //5.获取响应体的内容
                InputStream is = entity.getContent();
                System.out.println(is);
                //创建一个字节输出流，将图片输出到硬盘中"D/aa"目录
                //解析src获取图片的后缀名
                //int i1 = src.lastIndexOf(".");//得到的是最后一个 . 的索引,然后用substring来根据索引切割
                String sub = src.substring(src.lastIndexOf("."));
                //创建一个随时间毫秒值变化的的文件名
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmssSSS");
                String imgName = sdf.format(date) + sub;
                File dir = new File("D:/mzitu/" + mingzi);
                if (!dir.exists()) {// 判断目录是否存在
                    dir.mkdirs();
                }
                FileOutputStream out = new FileOutputStream(dir + "/" + imgName);//具体放在哪个地方可以由你自己确定,但是记得要这个文件夹一定要存在,否则会报错
                //将输入流中的内容拷贝到输出流
                IOUtils.copy(is, out);
                //关流
                out.close();
                is.close();


                System.out.println("下载ing.......");
            }
        }
    }
    System.out.println("本次下载完成了,快去打开吧...");
}




}
